package cn.ztydata.teachingsystem.heaven.web.controller;

import cn.ztydata.teachingsystem.common.Page;
import cn.ztydata.teachingsystem.heaven.common.WebContext;
import cn.ztydata.teachingsystem.heaven.cons.MsgTemplate;
import cn.ztydata.teachingsystem.heaven.entity.Speak;
import cn.ztydata.teachingsystem.heaven.entity.User;
import cn.ztydata.teachingsystem.heaven.service.SpeakService;
import cn.ztydata.teachingsystem.heaven.web.dto.speakComment.SpeakDto;
import cn.ztydata.teachingsystem.heaven.web.wrapper.HttpSessionProxy;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResult;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.text.MessageFormat;

/**
 * 发言控制类
 *
 * @author lyj
 * @since 2015-3-19
 */
@Controller
@RequestMapping("/talk")
public class SpeakController extends BaseController {
    @Autowired
    private SpeakService speakService;
    @Autowired
    private WebContext webContext;
    @Value("${session.key}")
    private String sessionKey;

    //<editor-fold desc="新增">
    /**
     * 新增发言
     *
     * @param speakDto 发言实体
     * @return JsonResult
     * @author lyj
     * @since 2015-3-21
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult addSpeak(@Valid @RequestBody SpeakDto speakDto) {
        Assert.notNull(speakDto.getGroupId(), MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "群组ID"));
        Assert.notNull(speakDto.getGroupType(), MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "群组类型"));

        speakService.addSpeak(mapper.map(speakDto, Speak.class));

        log.debug("新增发言");
        return new JsonResult(true, "新增发言成功！");
    }
    //</editor-fold>

    //<editor-fold desc="删除">
    /**
     * 删除发言
     *
     * @param id 发言ID
     * @return
     * @author lyj
     * @since 2015-3-21
     */
    @RequestMapping(value = "/delete/{id}/{groupType}/{groupId}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResult deleteSpeak(@PathVariable("id") long id,
                                  @PathVariable("groupType") int groupType,
                                  @PathVariable("groupId") long groupId,
                                  HttpServletRequest request) {
        Assert.isTrue(id > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "发言Id"));
        Assert.isTrue(groupType >= 0 && groupType < 4, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "群组类型"));
        Assert.isTrue(groupId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "群组ID"));

        //获取登录用户信息
        User userInfo = (User)((HttpSessionProxy)request.getSession()).getAttribute(sessionKey);

        speakService.deleteSpeak(id, groupType, userInfo);

        log.debug("删除发言");
        return new JsonResult(true, "删除发言成功！");
    }
    //</editor-fold>


    //<editor-fold desc="查询">
    /**
     * 获取发言列表
     *
     * @param groupType 群组类型（0非群组 1课程群组 2专业群组 3班级群组）
     * @param groupId 群组ID
     * @param pageNum 当前页码
     * @param pageSize 每页条数
     * @param tag tag标记（0全部 1我的）
     * @return
     * @author lyj
     * @since 2015-3-21
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultPage getList(@RequestParam("groupType") int groupType,
                                  @RequestParam("groupId") long groupId,
                                  @RequestParam("page") int pageNum,
                                  @RequestParam("pageSize") int pageSize,
                                  @RequestParam("tag") int tag,
                                  HttpServletRequest request){
        Assert.isTrue(groupType >= 0 && groupType <= 3, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "群组类型"));
        Assert.isTrue(groupId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "群组ID"));
        Assert.isTrue(pageNum > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "当前页码"));
        Assert.isTrue(pageSize > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "每页条数"));
        Assert.isTrue(tag >= 0 && tag < 2, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "tag标记"));

        //获取登录用户信息
        User userInfo = (User)((HttpSessionProxy)request.getSession()).getAttribute(sessionKey);

        Page<Speak> page = speakService.getList(groupType, groupId, pageNum, pageSize, tag, userInfo);

        log.debug("获取发言列表");
        return new JsonResultPage(new Page(page.getTotal(), pageSize, pageNum, mapper.mapList(page.getItems(), SpeakDto.class)));
    }
    //</editor-fold>
}
